const { error } = require('console')
const express = require('express')
const fs = require('fs')

// 创建应用对象
const app = express()

function myStatic(dir){
    return(req,res,next)=>{
        console.log('myStatic')
        let path = req.path
        if(path ==='/'){
            path = '/index.html'
        }
        const filePath = __dirname + '/' + dir + path
        fs.readFile(filePath,(error,data)=>{
            if(error){
                next()
            }else{
                res.send(data.toString())
            }
        })
    }
}
app.use(myStatic('public'))


app.use(express.urlencoded({extended:false}))
app.use(express.json())

app.get('/middleware', (req, res) => {
    res.send('middleware response')
  })

app.post('/middleware', (req, res) => {
    const{b,c} = req.body;
    console.log('请求体参数',b,c)
    res.send('post middleware response')
  })
  app.listen('8787',  () => {
    console.log('服务已启动: http://localhost:8787')
  })